﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using SS2TC.Lexicon;

namespace SS2TC.Speech
{
    /// <summary>
    /// Text to speech conversion.
    /// </summary>
    public class Pronouncer
    {
        private WaveFormat waveFormat = new WaveFormat();

        private Index lexicon;

        public Pronouncer(Index lexicon)
        {
            this.lexicon = lexicon;
        }

        /// <summary>
        /// Pronounce a textual representation of the sound.
        /// </summary>
        /// <param name="text">Textual representation of the sound.</param>
        public void Pronounce(string text)
        {
            AudioStreamWriter audioStreamWriter = new AudioStreamWriter(waveFormat);
            char[] characters = Encoding.Unicode.GetChars(Encoding.Unicode.GetBytes(text));

            foreach (char character in characters)
            {
                audioStreamWriter.Write(lexicon.Find(character.ToString()));
                Thread.Sleep(TimeSpan.FromMilliseconds(300));
            }

            audioStreamWriter.Close();
        }
    }
}
